package cn.hfatec.shms.hr.hrEmployee.action;

import cn.hfatec.shms.cache.CodeInfoCache;
import cn.hfatec.shms.enums.CodeType;
import cn.hfatec.shms.hr.hrContract.model.HrContractParams;
import cn.hfatec.shms.web.members.customer.model.CustomerParams;
import cn.hfatec.shms.web.vip.vipXxb.model.VipXxb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.Resource;


import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.hr.hrEmployee.model.HrEmployeeParams;
import cn.hfatec.shms.hr.hrEmployee.model.HrEmployee;
import cn.hfatec.shms.hr.hrEmployee.service.HrEmployeeService;
import cn.hfatec.shms.base.ShmsBaseAction;
import org.springframework.ui.Model;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;



/**
 *
 * @author hfatec codegen
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Controller
@RequestMapping("/hr/hrEmployee")
public class HrEmployeeAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "HrEmployeeService")
    private HrEmployeeService hrEmployeeService;


    /**
     *
     * 查询列表
     *
     */
    @RequestMapping("/list")
    public String list(HrEmployeeParams hrEmployeeParams, Page page, Model model) {

        Table table = hrEmployeeService.queryHrEmployeeTable(hrEmployeeParams, page);
        model.addAttribute("sexTypeList", CodeInfoCache.getCodeInfo(CodeType.SEX));
        model.addAttribute("employeeTypeList", CodeInfoCache.getCodeInfo(CodeType.EMPLOYEE_TYPE));
        model.addAttribute("hrEmployeesTable", table);
        model.addAttribute("hrEmployeeParams", hrEmployeeParams);
        return "/hr/hrEmployee/list";
    }

    /**
     *
     * 异步查询列表
     *
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(HrEmployeeParams hrEmployeeParams, Page page, Model model) {
        try {
            list(hrEmployeeParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/hr/hrEmployee/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到新增页
     *
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            HrEmployee hrEmployee = new HrEmployee();
            model.addAttribute("sexTypeList", CodeInfoCache.getCodeInfo(CodeType.SEX));
            model.addAttribute("employeeTypeList", CodeInfoCache.getCodeInfo(CodeType.EMPLOYEE_TYPE));
            model.addAttribute("hrEmployee", hrEmployee);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/hr/hrEmployee/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到编辑页
     *
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            HrEmployee hrEmployee = hrEmployeeService.getHrEmployee(id);
            model.addAttribute("sexTypeList", CodeInfoCache.getCodeInfo(CodeType.SEX));
            model.addAttribute("employeeTypeList", CodeInfoCache.getCodeInfo(CodeType.EMPLOYEE_TYPE));
            model.addAttribute("hrEmployee", hrEmployee);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/hr/hrEmployee/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 根据IDs删除数据
     *
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteHrEmployee(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if(ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            hrEmployeeService.deleteHrEmployeeBatch(idList);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     *
     * 保存数据
     *
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveHrEmployee(@Valid HrEmployee hrEmployee, BindingResult result) {

        try {
            if(result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            hrEmployeeService.saveHrEmployee(hrEmployee);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据HrEmployee保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }


    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxSelectList")
    @ResponseBody
    public String ajaxSelectList(HrEmployeeParams hrEmployeeParams, Model model, Page page) {
        try {
            list(hrEmployeeParams, page, model);
            model.addAttribute("customerParams", hrEmployeeParams);
            model.addAttribute("employeeTypeList", CodeInfoCache.getCodeInfo(CodeType.EMPLOYEE_TYPE));
            model.addAttribute("sexTypeList", CodeInfoCache.getCodeInfo(CodeType.SEX));
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/hr/hrEmployee/selectTable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到选择产品权益页
     */
    @RequestMapping("/dorefreshEmployee")
    @ResponseBody
    public String dorefreshEmployee(HrEmployeeParams hrEmployeeParams, Page page, Model model) {

        try {
            list(hrEmployeeParams, page, model);
            model.addAttribute("employeeTypeList", CodeInfoCache.getCodeInfo(CodeType.EMPLOYEE_TYPE));
            model.addAttribute("sexTypeList", CodeInfoCache.getCodeInfo(CodeType.SEX));
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/hr/hrEmployee/selectEmployee", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }



    /**
     * 跳转到客户视图
     */
    @RequestMapping("/goView/{employeeId}")
    public String goView(@PathVariable String employeeId, Model model) {
        HrEmployee hrEmployee = hrEmployeeService.getHrEmployee(employeeId);
        model.addAttribute("hrEmployee", hrEmployee);
   return "/hr/hrEmployee/view";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxEmployeeList")
    @ResponseBody
    public String ajaxEmployeeList(HrEmployeeParams hrEmployeeParams, Page page, Model model) {
        try {
            list(hrEmployeeParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/hr/hrEmployee/employeeTable", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
